<%@ page import="com.eyas.business.model.jpa.AdminInfo" %>
<%@ page import="com.eyas.business.config.webmvc.interceptor.LoginInterceptor" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta charset="utf-8">
    <title>角色管理</title>
    <link rel="shortcut icon" href="${pageContext.request.contextPath}/favicon.ico"/>
    <link rel="bookmark" href="${pageContext.request.contextPath}/favicon.ico"/>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/webjars/layui/css/layui.css" media="all">
    <script type="application/javascript" src="${pageContext.request.contextPath}/webjars/layui/layui.js"
            charset="utf-8"></script>
    <script type="application/javascript" src="${pageContext.request.contextPath}/webjars/jquery/jquery.js"></script>
    <script type="application/javascript"
            src="${pageContext.request.contextPath}/private/js/loading/ajaxloading.js"></script>
</head>
<body>
<fieldset class="layui-elem-field">
    <legend>查询条件</legend>
    <form id="form1" class="layui-form" action="">
        <table width="96%" align="center">
            <%--<colgroup>
                <col width="8%" align="center"/>
                <col width="23%" align="left"/>
                <col width="15%" align="center"/>
                <col width="23%" align="left"/>
                <col width="8%" align="center"/>
                <col width="13%" align="left"/>
            </colgroup>--%>
            <tr>
                <td align="center">角色名:</td>
                <td>
                    <input id="rolename" name="rolename" class="layui-input" type="text" placeholder="请输角色名"
                           autocomplete="off"
                           lay-verify="rolename">
                </td>
                <td align="center">有效状态:</td>
                <td>
                    <input id="validflag" name="validflag" type="checkbox" checked="" lay-skin="switch" value="1"
                           lay-text="有效|无效">
                </td>
                <td colspan="2" align="left">
                    <button class="layui-btn" lay-submit lay-filter="query">查询</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </td>
            </tr>
            <tr height="23"></tr>
        </table>
    </form>
</fieldset>

<fieldset class="layui-elem-field">
    <legend>查询结果</legend>
    <table class="layui-table" lay-filter="gridfilter"
           lay-data="{url:'',page:true, id:'gridresult',height:'330px',toolbar:'#toolbarDemo',defaultToolbar: ['filter']}">
        <thead>
        <tr>
            <th lay-data="{field:'roleid', width:'10%',hide:true}">角色id</th>
            <th lay-data="{field:'rolename', width:'20%',sort: true,align:'center'}">角色名称</th>
            <th lay-data="{field:'description', width:'40%',align:'center'}">角色描述</th>
            <th lay-data="{field:'validflag', width:'15%',templet: '#switchTpl',align:'center'}">显示状态</th>
            <th lay-data="{fixed: 'right',align:'center', toolbar: '#barDemo'}">操作</th>
        </tr>
        </thead>
    </table>

</fieldset>
</body>
<script id="switchTpl" type="text/html">
    <input type="checkbox" name="validflag" value="true" lay-skin="switch" lay-text="有效|无效" disabled {{
           d.validflag=='1'?'checked' : '' }}>
</script>
<script id="barDemo" type="text/html">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">{{ d.validflag=='1'?'置为无效' : '置为有效' }}</a>
    <% if(((AdminInfo)session.getAttribute(LoginInterceptor.LONGIN_SESSION_NAME)).getAdminnid()==1) { %>
    <a class="layui-btn layui-btn-xs" lay-event="remove">删除记录</a>
    <% } %>
</script>
<script id="toolbarDemo" type="text/html">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="addRoleInfo">新增角色</button>
    </div>
</script>
<script type="application/javascript">
    layui.use(['form', 'table'], function () {
        var form = layui.form;
        //switch off状态赋值
        form.on('switch', function (data) {
            $(data.elem).attr('type', 'hidden').val(this.checked ? '1' : '0');
        });

        //数据表格模块
        var table = layui.table;
        table.reload('gridresult', {
            url: '${pageContext.request.contextPath}/eyas/role/queryRoleInfo'
            , where: {validflag:'1'} //设定异步数据接口的额外参数
            , page: {
                curr: 1
            },
            method:'get'
        });
        //监听提交
        form.on('submit(query)', function (data) {
            table.reload('gridresult', {
                url: '${pageContext.request.contextPath}/eyas/role/queryRoleInfo'
                , where: data.field //设定异步数据接口的额外参数
                , page: {
                    curr: 1
                },
                method:'get'
            });
            return false;
        });
        //监听行工具事件
        table.on('tool(gridfilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                $.ajax({
                    type: "PUT",
                    url: "${pageContext.request.contextPath}/eyas/role/roleinfo/" + data.roleid+"/validflag",
                    dataType: "json",
                    success: function (data) {
                        layer.msg("修改成功");
                        obj.del();
                    },
                    error: function (xdata) {
                        layer.msg(xdata.responseJSON.errormsg);
                    }
                });
            } else if (obj.event === 'edit') {
                var win_1 = layer.open({
                    type: 2,
                    title: '用户角色编辑',
                    shadeClose: true,
                    shade: false,
                    maxmin: true, //开启最大化最小化按钮
                    area: ['100%', '100%'],
                    content: '${pageContext.request.contextPath}/eyas/role/edit?roleid='+data.roleid,
                    end:function() {
                        var rolename = $("#rolename").val();
                        var validflag = $("#validflag").val();
                        table.reload('gridresult', {
                            url: '${pageContext.request.contextPath}/eyas/role/queryRoleInfo'
                            , where: {rolename:rolename,validflag:validflag} //设定异步数据接口的额外参数
                            , page: {
                                curr: 1
                            },
                            method:'get'
                        });
                    }
                });
                layer.full(win_1);
            } else if(obj.event === 'remove') {
                if(data.validflag!='0') {
                    layer.msg("只能删除[无效]状态的数据");
                    return;
                }
                $.ajax({
                    type : "DELETE",
                    url : "${pageContext.request.contextPath}/eyas/admin/superadmin/roleinfo/roleid/"+data.roleid,
                    dataType : "json",
                    success: function(data) {
                        layer.msg("删除成功");
                        var rolename = $("#rolename").val();
                        var validflag = $("#validflag").val();
                        table.reload('gridresult', {
                            url: '${pageContext.request.contextPath}/eyas/role/queryRoleInfo'
                            , where: {rolename:rolename,validflag:validflag} //设定异步数据接口的额外参数
                            , page: {
                                curr: 1
                            },
                            method:'get'
                        });
                    },
                    error:function(xdata){
                        layer.msg(xdata.responseJSON.errormsg);
                    }
                });
            }
        });
        //头工具栏事件
        table.on('toolbar(gridfilter)', function(obj){
            if (obj.event === 'addRoleInfo') {
                var win_1 = layer.open({
                    type: 2,
                    title: '用户角色编辑',
                    shadeClose: true,
                    shade: false,
                    maxmin: true, //开启最大化最小化按钮
                    area: ['100%', '100%'],
                    content: '${pageContext.request.contextPath}/eyas/admin/pagegoto/roleinfo/edit',
                    end:function() {
                        var rolename = $("#rolename").val();
                        var validflag = $("#validflag").val();
                        table.reload('gridresult', {
                            url: '${pageContext.request.contextPath}/eyas/role/queryRoleInfo'
                            , where: {rolename:rolename,validflag:validflag} //设定异步数据接口的额外参数
                            , page: {
                                curr: 1
                            },
                            method:'get'
                        });
                    }
                });
                layer.full(win_1);
            }
        });

    });
</script>
</html>
